using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class JobSeekers_jobdetails : System.Web.UI.Page
{
    string connectionString;
     string jobID = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString =
                    WebConfigurationManager.ConnectionStrings[
                      "RecruitVietnam"].ConnectionString;
        
        RegisterJavaScript();
        
        this.hyperLinkPrint.NavigateUrl="#";
        Session["jsCurrentPage"] = 
        Request.ServerVariables.Get("URL")
        + "?" + Request.ServerVariables.Get(
        "QUERY_STRING"); ;

         
        jobID = Request.QueryString.Get("JobID");        
        if (!IsPostBack)
        {            
            try
            {
                
                
                lbnSendToFriend.PostBackUrl = 
                "~/JobSeekers/sendtofriend.aspx?page=" + 
                Request.ServerVariables.Get("SCRIPT_NAME") + 
                "?JobID=" + jobID;
                UpdateJobView();
                string fromPage = Request.QueryString.Get("page");                           
                if (jobID != null && jobID != "")
                {
                    int recruiterID = ShowDetails();
                    if (recruiterID>0)
                    {
                        
                        txtJobID.Text = jobID;
                        txtCompany.Text = DetailsView1.Rows[0].Cells[1].Text;
                        txtJobTitle.Text = DetailsView1.Rows[3].Cells[1].Text;
                        
                        this.hyperLinkJobs.NavigateUrl =
                        "JobResults.aspx?ID=" + recruiterID.ToString() +
                        "&value=" + txtCompany.Text + 
                        "&type=Company&page=company.aspx";

                        if (DetailsView1.Rows[27].Cells[1].Text != "&nbsp;")
                            Literal1.Text =
                            "<input type=button " +
                            "value='Apply online' " +
                            "onclick =\"window.open('" +
                            "http://www.huukhang.com/" +
                            "job.aspx?JobId=" +
                            DetailsView1.Rows[2].Cells[1].Text
                            + "');\">";
                            
                        AdjustData();
                    }
                    else
                    {
                        btnApply.Visible = false;
                        btnSave.Visible = false;
                        lbnSendToFriend.Visible = false;
                        hyperLinkPrint.Visible = false;
                        litError.Text = "Sorry, Job is not existing.";
                    }
                    txtASPXPage.Text = fromPage;
                }
                else
                    Response.Redirect("Search.aspx"); 
                
                 
            }
            catch (Exception ex)
            {
                litError.Text = ex.Message;
            }
        }
    }
    void AdjustData()
    {
        
        if (DetailsView1.Rows[16].Cells[0].Text != "")
            DetailsView1.Rows[16].Cells[0].Text
            = DetailsView1.Rows[16].Cells[
            0].Text.Replace("-", "<br>-");
        if (DetailsView1.Rows[18].Cells[0].Text != "")
            DetailsView1.Rows[18].Cells[0].Text =
            DetailsView1.Rows[18].Cells[
            0].Text.Replace("-", "<br>-");
    }
    void RegisterJavaScript()
    {
        Common common = new Common();
        this.ClientScript.RegisterClientScriptBlock(
        this.GetType(), "openWindow" +
        this.DetailsView1.ID,
        common.CreateScriptForPrint(this.DetailsView1.ID));
        this.hyperLinkPrint.Attributes.Add("onclick",
        "openWindow" + this.DetailsView1.ID
        + "('" + DetailsView1.ID.ToString()
        + "', 500, 500);return false;");
    }
    int ShowDetails( )
    {
        int isExisting = 0;
        try
        {
            SqlServerProvider obj = 
            new SqlServerProvider();
            obj.ConnectionString = 
            connectionString;
            obj.CommandText = 
            "SPViewJobDetails";
            obj.ParameterCollection = 
            new string[1] { "@JobID" };
            obj.ValueCollection = 
            new string[1] { jobID };
            obj.CommandType = 
            CommandType.StoredProcedure;
            DataTable dataTable = 
            obj.GetDataTable();
            if (dataTable.Rows.Count > 0)
            {
                isExisting = Convert.ToInt32(
                dataTable.Rows[0][1]);
                DetailsViewHelper objHelper = 
                new DetailsViewHelper();
                objHelper.FillDataTableToDetailsView(
                dataTable, this.DetailsView1);
                literalIntro.Text = 
                dataTable.Rows[0][0].ToString();
            }
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
        return isExisting;
    }
    void UpdateJobView( )
    {
        RecruitVietnam recruitVietnam = 
        new RecruitVietnam();  
        recruitVietnam.ConnectionString = 
        connectionString;
        recruitVietnam.InreaseJobView(jobID);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (Convert.ToString(
                        Session["jsuserid"]) == "")
                Response.Redirect("login.aspx");
           
            RecruitVietnam recruitVietnam = 
            new RecruitVietnam();
            recruitVietnam.ConnectionString 
            = connectionString;
            recruitVietnam.SaveJob(
            Convert.ToString(
            Session["jsuserid"]), jobID);
        }
        catch(Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
    
}
